package com.yocard.yxyd.ui.presenter;

import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.base.Response;
import com.yocard.yxyd.http.model.request.PushSettingRequest;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.InviteResponse;
import com.yocard.yxyd.http.model.response.PushSettingBean;
import com.yocard.yxyd.ui.contract.PushSettingContract;
import com.yocard.yxyd.utils.RxUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import static com.yocard.yxyd.event.EventCode.REFRESH_TOKEN;

/**
 * Created on 2019/4/9.
 */
public class PushSettingPresenter extends RxPresenter<PushSettingContract.View> implements PushSettingContract.Presenter {

    @Inject
    public PushSettingPresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void savePushParameters(PushSettingRequest request) {
        addSubscribe(httpManager.savePushParameters(request).compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView) {

                    @Override
                    public void onNext(Response<String> tResponse) {
                        if (tResponse.getCode() == Constants.STATUS_SUCCESS_1
                                || tResponse.getCode() == Constants.STATUS_SUCCESS_2
                                || tResponse.getCode() == Constants.STATUS_SUCCESS_3) {
                            onSuccess(tResponse.getMessage());
                        } else if (tResponse.getCode() == Constants.STATUS_LOGIN_MUTI) {
                            EventBus.getDefault().post(new CommonBean(REFRESH_TOKEN));
                        } else {
                            mView.showErrorMsg(tResponse.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String bannerBean) {
                        try {
                            mView.showErrorMsg(bannerBean);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }

    @Override
    public void getPushParameters() {
        addSubscribe(httpManager.getPushParameters().compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<PushSettingBean>(mView) {
                    @Override
                    public void onSuccess(PushSettingBean bannerBean) {
                        try {
                            mView.onGetPushParameters(bannerBean);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }
}
